//
//  ViewController.m
//  UI15_SandBox_NSFileManager_NSFileHandler
//
//  Created by 李劲成 on 16/3/29.
//  Copyright © 2016年 ljc. All rights reserved.
//

#import "ViewController.h"
#import "SandBoxHandler.h"
#import "ModelOfContact.h"
@interface ViewController ()

@property (nonatomic, copy)NSString *pathOfString;

@property (nonatomic, copy)NSString *pathOfArray;

@property (nonatomic, copy)NSString *pathOfDic;

@property (nonatomic, copy)NSString *pathOfData;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleSandBox];
    [self saveFundationObject];
    [self readFundationObject];
    [self saveModelToDisk];
    [self readModelFromDisk];
    // Do any additional setup after loading the view, typically from a nib.
}


#pragma mark - 知识点1: SandBox (沙盒)
/** 详见 SandBoxHandler 类. */
- (void)handleSandBox {
    SandBoxHandler *sand = [SandBoxHandler sandBoxHandler];
    NSLog(@"%@", sand.pathForDoucuments);
//    NSLog(@"%@", sand.pathForTmp);
//    NSLog(@"%@", sand.pathForCaches);
//    NSLog(@"%@", sand.pathForLibrary);
//    NSLog(@"%@", sand.pathForAppSandbox);
//    NSLog(@"%@", sand.pathForApp);
//    NSLog(@"%@", [sand pathForPreferences]);
}

#pragma mark - 知识点2: 简单对象写入/读取(磁盘)
/** 
  * 简单对象: NSFundation 框架中的类.
  * Core API:writetoFile:(写入)
  */
- (void)saveFundationObject {
    [self saveNSString];
    [self saveNSArray];
    [self saveDictionary];
    [self saveData];
}
- (void)readFundationObject {
    [self readStringFromDisk];
    [self readArrayFromDisk];
    [self readDicFromDisk];
    [self readDataFromDisk];
}

/** 保存字符串 */
- (void)saveNSString {
    NSString *str = @"dalian SoftPark #10 Building";
    // 文件路径.
    self.pathOfString = [SandBoxHandler saveInDocumentsWithFileName:@"title.text"];
     // Core API : writeToFile
    [str writeToFile:self.pathOfString atomically:YES encoding: NSUTF8StringEncoding error:nil];


}
- (void)readStringFromDisk {
    NSString *str = [NSString stringWithContentsOfFile:self.pathOfString encoding:NSUTF8StringEncoding error:nil];
 
}

/** 保存数组 */
- (void)saveNSArray {
    NSArray *arr = @[@"zhangsan", @"lisi", @"Tom"];
    self.pathOfArray = [SandBoxHandler saveInDocumentsWithFileName:@"names.plist"];
    [arr writeToFile:self.pathOfArray atomically:YES];
    
}
- (void)readArrayFromDisk {
    // 读取
    // 1.找到文件路径.
    NSArray *arr = [NSArray arrayWithContentsOfFile:self.pathOfArray];
    NSLog(@"%@", arr);
}

/** 保存字典 */
- (void)saveDictionary {
    // 1.要保存的字典.
    NSDictionary *dic = @{@"name":@"zhangsan",
                          @"age":@"21",
                          @"sex":@"male"};
    self.pathOfDic = [SandBoxHandler saveInDocumentsWithFileName:@"dic.plist"];
    [dic writeToFile:self.pathOfDic atomically:YES];
    

}

- (void)readDicFromDisk {
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:self.pathOfDic];
    NSLog(@"%@", dic);
}

- (void)saveData {
    // 数据对象.
    UIImage *image = [UIImage imageNamed:@"QQ20160326-0@2x.png"];
    
    // API: 函数, 将 image对象转换为Data 对象.
    NSData *data = UIImageJPEGRepresentation(image, 1);
    
    self.pathOfData = [SandBoxHandler saveInDocumentsWithFileName:@"data.jpg"];
    [data writeToFile:self.pathOfData atomically:YES];
    

}

- (void)readDataFromDisk {
    NSData *data = [NSData dataWithContentsOfFile:self.pathOfData options:NSDataReadingMappedIfSafe error:nil];
    UIImage *image = [UIImage imageWithData:data];
}


#pragma mark - 知识点3: 复杂对象写入/读写 (磁盘)
/** 对象想进行归档, 必须签订 NSCoding 协议, 并实现两个协议方法. */
- (void)saveModelToDisk {
    ModelOfContact *con1 = [[ModelOfContact alloc]init];
    con1.name = @"cuixiaojiangjun";
    con1.age = 18;
    
    // 归档
    NSMutableData *mData = [NSMutableData data];
    NSKeyedArchiver *archievr = [[NSKeyedArchiver alloc]initForWritingWithMutableData:mData];
    [archievr encodeObject:con1 forKey:@"model"];
    // 完成编码.
    [archievr finishEncoding];
    
    [mData writeToFile:[SandBoxHandler saveInDocumentsWithFileName:@"model.da"] atomically:YES];

}

- (void)readModelFromDisk {
    NSMutableData *data = [NSMutableData dataWithContentsOfFile:[SandBoxHandler saveInDocumentsWithFileName:@"model.da"]];
    
    // 反归档
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    ModelOfContact *model = [unarchiver decodeObjectForKey:@"model"];
    // 完成解码
    [unarchiver finishDecoding];
    NSLog(@"%@", model.name);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
